package com.xiao.controller;


import com.xiao.domain.ResponseResult;
import com.xiao.enums.AppHttpCodeEnum;
import com.xiao.exception.SystemException;
import com.xiao.domain.pojo.BlogSysUser;
import com.xiao.service.BlogLoginService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

@RestController
@Api(tags = "博客登录->控制层")
public class BlogLoginController {
    @Autowired
    BlogLoginService blogLoginService;
    // 登录请求
    @PostMapping("/login")
    @ApiOperation("用户登录请求")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "blogSysUser",value = "用户实体类")
    })
    public ResponseResult login(@RequestBody BlogSysUser blogSysUser){ // 前端传递来的参数用 实体类来接

/*        // ----------- 原始写法 Start -----------
       //如果字符序列不为null值,并且字符序列的长度大于0,并且不含有空白字符序列,则返回true
        if (!StringUtils.hasText(blogSysUser.getUserName())){// 使用 springframework 包下
             // 提示 必须要传入用户名
            throw new RuntimeException("必须填写用户名");
        }
        // ----------- 原始写法 End -----------*/


        // ----------- 通常写法 Start -----------
        if (!StringUtils.hasText(blogSysUser.getUserName())){
            // 提示 必须要传入用户名
            throw new SystemException(AppHttpCodeEnum.USERNAME_NOT_NULL);
        }
        // ----------- 通常写法 End -----------


        ResponseResult responseResult = blogLoginService.login(blogSysUser);
        return responseResult;
    }

    @PostMapping("/logout")
    @ApiOperation("用户注销请求")
    public ResponseResult logout(){
        ResponseResult result = blogLoginService.logout();
        return result;
    }


}